package com.ad.common.dao;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;

import com.ad.common.entity.User;

/**
 	* A data access object (DAO) providing persistence and search support for User entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .User
  * @author MyEclipse Persistence Tools 
 */

public class UserDAO extends BaseDAO  {
    private static final Log log = LogFactory.getLog(UserDAO.class);
	//property constants
	public static final String USERNAME = "username";
	public static final String PASSWORD = "password";
	public static final String LAST_IP = "lastIp";
	public static final String RANK_ID = "rankId";
	public static final String LOCKED = "locked";
	public static final String USER_TYPE = "userType";
	
    public boolean save(User transientInstance) {
        log.debug("saving User instance");
        boolean saveRet = true ;
        try {
        	this.getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            saveRet = false;
            throw re;
        }
        return saveRet ;
    }
    
	public void delete(User persistentInstance) {
        log.debug("deleting User instance");
        try {
        	this.getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    public List<User> findByProperties( String queryString ,Object[] values){
    	 log.debug("getting User instance with name=: " + queryString);
         try {
            List<User> instances = this.getHibernateTemplate().find(queryString, values);
             return instances;
         } catch (RuntimeException re) {
             log.error("get failed", re);
             throw re;
         }
    }
    public User loginQuery(String username,String password,int userType){
    	 log.debug("getting User instance with username: " + username+" password"+password);
         try {
            List<User> users = this.findByProperties("from User as model where model.userType=? and model.username=? and model.password=?", new Object[]{userType,username,password});
            if(users!=null && users.size()==1){
            	User user = users.get(0);
            	user.setLastLoginTime(new Date());
            	this.merge(user);
            	return user;
            }
         } catch (RuntimeException re) {
             log.error("get failed", re);
             throw re;
         }
         return null ;
    }
    public User findById( java.lang.Integer id) {
        log.debug("getting User instance with id: " + id);
        try {
            User instance =(User) this.getHibernateTemplate().get("User", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(User instance) {
        log.debug("finding User instance by example");
        try {
        	 List results = this.getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding User instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from User as model where model." 
         						+ propertyName + "= ?";
         return getHibernateTemplate().find(queryString, value);
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public User findByUsername(Object username
	) {
		List list =  findByProperty(USERNAME, username);
		if(list==null || list.size()==0){
			return null ;
		}else{
			return (User)list.get(0);
		}
	}
	
	public List findByPassword(Object password
	) {
		return findByProperty(PASSWORD, password
		);
	}
	
	public List findByLastIp(Object lastIp
	) {
		return findByProperty(LAST_IP, lastIp
		);
	}
	

	public List findByRankId(Object rankId
	) {
		return findByProperty(RANK_ID, rankId
		);
	}
	
	public List findByLocked(Object locked
	) {
		return findByProperty(LOCKED, locked
		);
	}
	
	public List findByUserType(Object userType
	) {
		return findByProperty(USER_TYPE, userType
		);
	}
	

	public List findAll() {
		log.debug("finding all User instances");
		try {
			String queryString = "from User";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public User merge(User detachedInstance) {
        log.debug("merging User instance");
        try {
            User result = (User) getHibernateTemplate().merge(
					detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(User instance) {
        log.debug("attaching dirty User instance");
        try {
        	getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(User instance) {
        log.debug("attaching clean User instance");
        try {
        	getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
	public void saveOrUpdate(User instance){
		log.debug("update User instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
		} catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
	}
    public static void main(String[] args){
    
    	UserDAO dao = new UserDAO();
//    	UserInfoDAO dao1 = new UserInfoDAO();
    	dao.getSession().getTransaction().begin();
    	dao.getSession().getTransaction().commit();
    	
    }
}